<template>
	<view class="container">
		<!-- 背景图 -->
		<view class="x-c"><image class="logo-bg" src="http://shopro.7wpp.com/imgs/logo_bg.png" mode=""></image></view>
		<!-- titleview -->
		<view class="head-box"><cu-custom :isBack="true"></cu-custom></view>
		<view class="wrapper">
			<!-- logo -->
			<view class="x-c">
				<text class="tab-title" style="font-size:46rpx">用户注册</text>
				<!-- <image class="logo" :src="sysInfo.logo" mode="widthFix"></image> -->
			</view>
			<!-- 表单 -->
			<view class="login-box y-f">
				<view class="input-item x-c">
					<text class="inp-title">手机号</text>
					<input class="inp" v-model="mobile" type="number" placeholder="请输入手机号" placeholder-class="pl" />
				</view>
				<!-- <view class="input-item x-c">
					<text class="inp-title">验证码</text>
					<input class="inp" v-model="code.value" type="number" placeholder="请输入验证码" placeholder-class="pl" />
					<button class="cu-btn code-btn" :disabled="code.status" @tap="getCode">{{ code.text }}</button>
				</view> -->
				<view class="input-item x-c">
					<text class="inp-title">密&emsp;码</text>
					<input class="inp" password v-model="password" type="text" placeholder="请输入6-14位密码" placeholder-class="pl" />
				</view>
				<view class="input-item x-c">
					<text class="inp-title">确认密码</text>
					<input class="inp" password v-model="password2" type="text" placeholder="请输入6-14位密码" placeholder-class="pl" />
				</view>
				<view class="tip-box" @tap="onTcp">
					<label class="x-f">
						<radio class="tcp-radio brown" :class="{ checked: isTcp }" :checked="isTcp"></radio>
						<view class="">
							我已阅读并遵守
							<text class="tcp" @tap="jump('/pages/public/richtext', { id: 1 })">《用户协议》</text>
							与
							<text class="tcp" @tap="jump('/pages/public/richtext', { id: 2 })">《隐私协议》</text>
						</view>
					</label>
				</view>
			</view>
			<!-- 登录按钮 -->
			<view class="x-c y-f"><button :disabled="isdisable" class="cu-btn login-btn" @tap="register">立即注册</button></view>
		</view>
	</view>
</template>

<script>
import hex_md5 from "@/common/js/md5.js";
import { mapMutations, mapActions, mapState } from 'vuex';

export default {
	data() {
		return {
			mobile: '',
			code: {
				text: '获取验证码',
				status: false,
				value: ''
			},
			password: '',
			password2:'',
			isTcp: false, //协议
			isdisable:false,
			sysInfo: uni.getStorageSync('sysInfo')
		};
	},
	computed: {
		...mapState({
			initData: state => state.init.initData
		})
	},
	onLoad(option) {
		if(option.puid){
			uni.setStorageSync("pUid",option.puid);
			
			
			uni.request({
			    url: this.$config.API_URL+"User/GetUserInfoByUid",
				method:"POST",
				data:{
					uid:option.puid
				},
				success: (result) => {
					let storeId=result.data[0].storeid;
					let orgcode=result.data[0].orgcode;
					
					uni.setStorageSync("orgcode",orgcode);
					uni.setStorageSync("storeid",storeId);
				}
			})
			
			
			
		}
	},
	methods: {
		...mapActions(['setTokenAndBack']),
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
		async getCode() {
			let that = this;
			that.code.status = true;
			let countdown = 60;
			
			
			
			that.$api('sms.send', {
				mobile: that.mobile,
				event: 'register'
			}).then(res => {
				if (res.code === 1) {
					that.code.text = countdown + '秒';
					that.code.status = true;
					let timer = setInterval(() => {
						if (countdown > 0) {
							that.code.text = countdown - 1 + '秒';
							countdown--;
						} else {
							clearInterval(timer);
							that.code.text = '获取验证码';
							that.code.status = false;
						}
					}, 1000);
				} else {
					that.code.status = false;
				}
			});
		},

		// register() {
		// 	let that = this;
		// 	if (this.isTcp) {
		// 		this.$api('user.register', {
		// 			mobile: this.mobile,
		// 			code: this.code.value,
		// 			password: this.password
		// 		}).then(res => {
		// 			if (res.code === 1) {
		// 				uni.showToast({
		// 					title: res.msg || '注册成功,请前往登录',
		// 					icon: 'success',
		// 					duration: 1000,
		// 					mask: true,
		// 					success: function() {
		// 						that.setTokenAndBack(res.data.userinfo.token);
		// 					}
		// 				});
		// 			}
		// 		});
		// 	} else {
		// 		this.$tools.toast('请先同意协议！');
		// 	}
		// },
		register() {
			let that = this;
			let puid=uni.getStorageSync("pUid");
			let owner_uid=uni.getStorageSync("owner_uid");
			let orgcode=uni.getStorageSync("orgcode")||"000003";
			let storeid=uni.getStorageSync("storeid");
						
			let f_uid=(puid||owner_uid);
			if(that.mobile=='')	{
				uni.showToast({title:  '手机号码不能为空',icon: 'none'});
				return;
			}
			if(that.password=='')	{
				uni.showToast({title:  '密码不能为空',icon: 'none'});
				return;
			}
			if(that.password!=that.password2)	{
				uni.showToast({title:  '两次密码输入不一致',icon: 'none'});
				return;
			}
			
						
			that.isdisable=true;
			if (this.isTcp) {
				
				
				
				
				
				let RegEOSUser=function(){
				//平台注册
				 return new Promise(function(resolve,reject){
							var postObj = {
								method: "Base.MicroGroup.User.Register",
								ver: "1.0",
								timestamp: parseInt(Date.parse(new Date()) / 1000 + 8 * 60 * 60).toString(),
								username: that.mobile,
								password: hex_md5(that.password),
								// groupguid:groupguid,
								orgcode:orgcode,
								name:that.mobile
							};
							
							
							//注册成为平台用户
							uni.request({
								url: that.$config.API_Gateway,
								method:"POST",
								data:postObj,
								success: (result) => {	
									console.log(result);
									
									if(result.data.success==false){
										if(result.data.msg.errmsg=="user_exist"){
											if(reject)reject('用户已存在');	
											return;
										}
										
									}
									let newUser={};
									newUser=result.data;
									newUser.Password= hex_md5(that.password);
									newUser.DistributeCode="";
									newUser.orgcode=orgcode;
									newUser.storeid=storeid;
									newUser.puid=puid;
									
										resolve(newUser);
								},
								fail:()=>{
									if(reject)reject();
								}
							});
							
							
						});
				}
				
				let RegShopUser=function(newUser){
					return new Promise(function(resolve,reject){
						//注册成为商城用户
						uni.request({
						    url: that.$config.API_URL+"User/CreateShopUser",
							method:"POST",
							data:newUser,
							success:(regResult)=>{
								if(!regResult.data.msg||!regResult.data.msg.uid){
									reject();
									return;
								}
								
								let uid=regResult.data.msg.uid;
								
								resolve(uid);
							},
							fail:()=>{
							}
						});
					})
					
				}
				
				let RegDistributeLink=function(uid){
					// if(puid!=''){
						return new Promise(function(resolve,reject){							
							uni.request({
								url: that.$config.Distribute_URL+"write_regis/?userid="+uid+"&f_uid="+(puid||owner_uid)+"&phonenum="+that.mobile,
								method:"GET",
								success:(pyResult)=>{
									// alert(pyResult);									
									resolve(uid);
								},
							})
						})
					// }else{
					// 	return new Promise(function(resolve,reject){
					// 		resolve(uid);
					// 	})
					// }
				}
				
				let RegDistributeBenefit=function(uid){
					// if(puid!=''){
					if(puid=='')puid=9679;
						return new Promise(function(resolve,reject){
							uni.request({
								url: that.$config.Distribute_URL+"write_regis_benefit_dic/?userid="+uid+"&register_id="+uid,
								method:"GET",
								success:(pyResult)=>{
									
									
									resolve();
									
									
								},
							})
						})
					// }else{
					// 	return new Promise(function(resolve,reject){
					// 		resolve(uid);
					// 	})
					// }
				}
				
				let RegDistributeLinkNew=function(uid){
					return new Promise(function(resolve,reject){
						uni.request({
							url: that.$config.Distribute_URL+"writereginfo/?dbname=Retail_Store"+storeid+"&userid="+uid+"&phonenum="+that.mobile+"&f_uid="+(puid||owner_uid)+"&register_id="+uid,
							method:"GET",
							success:(pyResult)=>{
								
								
								resolve();
								
								
							},
						})
					});
				}
				
				let RegDistributeBenefitNew=function(uid){
					return new Promise(function(resolve,reject){
						uni.request({
							url: that.$config.Distribute_URL+"writereginfo_fanyong/?dbname=Retail_Store"+storeid+"&userid="+uid+"&register_id="+uid,
							method:"GET",
							success:(pyResult)=>{
								
								
								resolve();
								
								
							},
						})
					});
				}
				
				
				
				let showSuccess=function(){
					uni.showToast({
						title:  '注册成功,请前往登录',
						icon: 'success',
						duration: 1000,
						mask: true,
						success: function() {
							window.setTimeout(function(){
								that.isdisable=false;
								that.jump("/pages/public/login",{})
							},2000);
							
						}
					}); 
				}
				
				let showFail=function(msg){
					uni.showToast({
						title:  msg?msg:"注册失败",
						icon: 'none',
						duration: 1000,
						mask: true,
						success: function() {
							that.isdisable=false;
						}
					}); 
				}
				
				
				RegEOSUser()
				.then(RegShopUser)
				// .then(RegDistributeLink)
				// .then(RegDistributeBenefit)
				.then(RegDistributeLinkNew)
				.then(RegDistributeBenefitNew)
				.then(showSuccess)
				.catch(showFail)

			} else {
				this.$tools.toast('请先同意协议！');
				that.isdisable=false;
			}
		},
		onTcp() {
			this.isTcp = !this.isTcp;
		}
	}
};
</script>

<style lang="scss">
.container {
	position: relative;
	width: 100vw;
	height: 100vh;
	// overflow: hidden;
	background: linear-gradient(180deg, rgba(239, 196, 128, 1) 0%, rgba(248, 220, 165, 1) 25%, rgba(255, 255, 255, 1) 98%);

	// titleview
	.head-box {
		.cuIcon-back {
			font-size: 50rpx !important;
			font-weight: 500;
		}
	}

	// logo
	.logo {
		width: 410rpx;
		height: 120rpx;
	}

	.logo-bg {
		width: 640rpx;
		height: 300rpx;
	}
}

.wrapper {
	position: absolute;
	z-index: 90;
	padding-bottom: 40upx;
	padding-top: 115px;
	width: 100vw;
	height: 100vh;
	top: 0;

	// 输入
	.login-box {
		margin-top: 140rpx;

		.input-item {
			height: 108rpx;
			border-bottom: 1rpx solid rgba(#d0b17b, 0.3);
			width: 608rpx;

			.inp-title {
				font-size: 30rpx;
				width:140rpx;
				color: #845708;
				padding-right: 30rpx;
			}

			.inp {
				flex: 1;
				height: 100%;
				font-size: 28rpx;
			}

			.pl {
				color: #c8963d;
			}

			.code-btn {
				background: none;
				font-size: 28rpx;
				color: #845708;
				border-left: 1rpx solid #d0b17b;
				border-radius: 0;
				height: 42rpx;
				width: 190rpx;
				padding: 0;
			}
		}
	}

	// 登录
	.login-btn {
		width: 630upx;
		height: 80upx;
		border-radius: 40rpx;
		margin-top: 70upx;
		background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
		box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
		color: #fff;
	}

	.tip-box {
		width: 630rpx;
		line-height: 108rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(200, 150, 61, 1);

		.tcp-radio {
			transform: scale(0.5);
		}

		.tcp {
			text-decoration: underline;
		}
	}
}
</style>
